/*
 Copyright 2009 Abdulla Abdurakhmanov (abdulla@latestbit.com)
 Original sources are available at www.latestbit.com

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.

 SmartBus.hpp. Created on: 17.09.2009 16:11:44
 */

#ifndef SMARTBUS_HPP_
#define SMARTBUS_HPP_

#include "SmartObjectsDefs.hpp"
#include "ISmartBus.hpp"

namespace smartobjects {

class SmartBus : public ISmartBus {
public:
	SO_DLLPRFX SmartBus();
	SO_DLLPRFX virtual ~SmartBus();

	SO_DLLPRFX virtual IDataTypesCoder* createDataTypesCoder() ;
	SO_DLLPRFX virtual cpputils::utils::SequenceGenerator<long>& getSequenceGenerator() ;

	SO_DLLPRFX virtual IManagedNetwork* getManagedNetwork() ;
	SO_DLLPRFX virtual void setManagedNetwork(IManagedNetwork* network) ;

	SO_DLLPRFX virtual void setConnectMode(ConnectMode mode) ;
	SO_DLLPRFX virtual ConnectMode getConnectMode() ;
	SO_DLLPRFX virtual void setSyncConnectTimeout(unsigned long seconds) ;
	SO_DLLPRFX virtual unsigned long getSyncConnectTimeout() ;
	SO_DLLPRFX virtual cpputils::ept::ThreadPool* getThreadPool() ;
	SO_DLLPRFX virtual IServerInstancesRegistry* getServerInstancesRegistry() ;
	SO_DLLPRFX virtual SmartBusClient* selectBusClient(IEndpointLocation* poLocation) ;
	SO_DLLPRFX virtual SmartBusServer* selectBusServer(IEndpointLocation* poLocation) ;

	SO_DLLPRFX virtual IEndpointCoordinatorFactory* getCoordinatorFactory() ;
	SO_DLLPRFX virtual void setCoordinatorFactory(IEndpointCoordinatorFactory* poFactory) ;
	SO_DLLPRFX virtual IDataEncrypterFactory* getDataEncrypterFactory() ;
	SO_DLLPRFX virtual void setDataEncrypterFactory(IDataEncrypterFactory* poFactory) ;
	SO_DLLPRFX virtual void setHandshakeManagerFactory( IHandshakeManagerFactory* poHandshakeManagerFactory ) ;
	SO_DLLPRFX virtual IHandshakeManagerFactory* getHandshakeManagerFactory() ;

	SO_DLLPRFX virtual IEndpointLocation* createEndpointLocation(const std::string& sReferenceURI) ;

protected:
	SO_DLLPRFX static const unsigned int DEFAULT_CONNECT_TIME;

	SO_DLLPRFX static const unsigned int DEFAULT_THREAD_POOL_INITIAL_CNT;
	SO_DLLPRFX static const unsigned int DEFAULT_THREAD_POOL_MAX_CNT;
	SO_DLLPRFX static const std::string SMART_BUS_THREAD_POOL_ID;

	SO_DLLPRFX virtual void returnBusClient(SmartBusClient* poClient);
	SO_DLLPRFX virtual void returnBusServer(SmartBusServer* poLocation);

protected:
	SO_DLLPRFX virtual void registerStdManagerFactories();

protected:
	IManagedNetwork* m_poManagedNetwork;
	IDataEncrypterFactory* m_poDataEncrypterFactory;
	IHandshakeManagerFactory* m_poHandshakeManagerFactory;
	IPacketFormerFactory* m_poPacketFormerFactory;
	IEndpointCoordinatorFactory* m_poEndpointCoordinatorFactory;
	IServerInstancesRegistry* m_poInstanceRegistry;
	cpputils::ept::ThreadPool* m_poThreadPool;

	cpputils::utils::SequenceGenerator<long> m_oSequenceGenerator;
	ConnectMode m_nConnectMode;
	unsigned int m_nAwaitConnectTime;

	typedef boost::bimap < std::string, SmartBusClient*, boost::bimaps::with_info< unsigned int > > BusClientsMap;
	BusClientsMap m_mClients;
	boost::recursive_mutex m_mxClients;

	typedef boost::bimap < std::string, SmartBusServer*, boost::bimaps::with_info< unsigned int > > BusServersMap;
	BusServersMap m_mServers;
	boost::recursive_mutex m_mxServers;

};

}

#endif /* SMARTBUS_HPP_ */
